package com.itheima.health.controller;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dto.OrderSubmitParam;
import com.itheima.health.dto.SmsLoginParam;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.util.SendMessageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/mobile")
@Slf4j
public class MobileController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private SetmealService setmealService;

    //静态属性
    private static String validateCode;


    /**
     * 发送验证码
     *
     * @param type
     * @param telephone
     * @return
     * @throws ClientException
     * @throws com.aliyuncs.exceptions.ClientException
     */
    @PostMapping("/validateCode/send")
    public Result validateCode(@RequestParam String type, @RequestParam String telephone) throws ClientException {
        RedisConst redisConst = new RedisConst();
        //发送消息,发送新的验证码telephone
        SendMessageUtils.setNewcode();
        //获取一个为整形的验证码
        String code = Integer.toString(SendMessageUtils.getNewcode());
        System.out.println("发送的验证码为:{}" + code);

        SendSmsResponse response = SendMessageUtils.sendSms(telephone, code);
        System.out.println("短信接口返回的数据------------");
        System.out.println("Code=" + response.getCode());
        System.out.println("Message" + response.getMessage());
        System.out.println("RequestId" + response.getRequestId());

        validateCode = code;

        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }

    /**
     * 登录
     *
     * @param smsLoginParam
     * @return
     */
    @PostMapping("/login/smsLogin")
    public Result smsLogin(@RequestBody SmsLoginParam smsLoginParam) {

        if (smsLoginParam.getValidateCode().equals(validateCode)) {
            return new Result(true, MessageConst.LOGIN_SUCCESS);
        }
        return new Result(false, MessageConst.LOGIN_FAIL);
    }


    /**
     * 体检套餐展示
     *
     * @param
     * @return
     */
    @GetMapping("/setmeal/getSetmeal")
    public Result getSetmeal() {

        List<Setmeal> setmeals = setmealService.getSetmeal();
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmeals);
    }

    /**
     * 套餐详情
     * @param id
     * @return
     */
    @GetMapping("/setmeal/findById")
    public Result findById(Integer id) {

        List<CheckGroup> checkGroups = setmealService.findGroupsBySetmealId(id);

        Setmeal setmeal = setmealService.selectById(id);
        setmeal.setCheckGroups(checkGroups);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmeal);
    }

    /**
     * 预约下单
     * @param orderSubmitParam
     * @return
     */
    @PostMapping("/order/submit")
    public Result submit(@RequestBody OrderSubmitParam orderSubmitParam){

        Order order = orderService.add(orderSubmitParam);

        return new Result(true,MessageConst.ORDER_SUCCESS,order);
    }

    /**
     * 订单详情
     * @param id
     * @return
     */
    @GetMapping("/order/findById")
    public Result orderFindById(Integer id){
        Order order = orderService.selectById(id);
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,order);
    }
}
